package com.papercut.service;

import java.util.List;

import com.papercut.purchase.PurchaseEntity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PaperCutDatabase extends SQLiteOpenHelper{

	private static final String DATABASE_NAME = "paper.db";
	
	private static final int DATABASE_VERSION = 4;
	
	private static final String TABLE_NAME = "notificationName";
	
	private static final String ID = "id";
	
	private static final String TRANSACTION_ID = "transactionID";
	
	private static final String MERCHANT = "merchantName";
	
	private static final String INVOICE_DATE = "invoiceDate";
	
	private static final String PURCHASE_DATE = "purchaseDate";
	
	private static final String HAS_PURCHASED = "hasPurchased";
	
	private static final String TOTAL_COST = "totalCost";
	
	public PaperCutDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
        db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + TRANSACTION_ID + " TEXT,"
                + MERCHANT + " TEXT,"
                + INVOICE_DATE + " INTEGER,"
                + PURCHASE_DATE + " INTEGER,"
                + HAS_PURCHASED + " INTEGER,"
                + TOTAL_COST + " FLOAT);");
        
        
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
        // Kills the table and existing data
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);

        // Recreates the database with a new version
        onCreate(db);
	}
	
	
	public void newNotification(String transactionID, String merchantName, int invoiceDate, float totalCost, int purchaseDate){
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues content = new ContentValues();
		content.put(TRANSACTION_ID, transactionID);
		content.put(MERCHANT, merchantName);
		content.put(INVOICE_DATE, invoiceDate);
		content.put(PURCHASE_DATE, purchaseDate);
		content.put(HAS_PURCHASED, 0);
		content.put(TOTAL_COST, totalCost);
		
		db.insert(TABLE_NAME, null, content);
						
	}
	
	public void getHistory(List<String> transactionID,
			List<String> merchantName, List<Integer> invoiceDate,
			List<Integer> purchaseDate, List<Integer> hasPurchased,
			List<Float> totalCost) {
		SQLiteDatabase db = this.getReadableDatabase();

		String[] columns = { TRANSACTION_ID, MERCHANT, INVOICE_DATE,
				PURCHASE_DATE, HAS_PURCHASED, TOTAL_COST };
		Cursor result = db.query(TABLE_NAME, columns, null, null, null, null,
				null);

		if (result.moveToFirst()) {
			do {
				transactionID.add(result.getString(result
						.getColumnIndex(TRANSACTION_ID)));
				merchantName.add(result.getString(result
						.getColumnIndex(MERCHANT)));
				invoiceDate.add(result.getInt(result
						.getColumnIndex(INVOICE_DATE)));
				purchaseDate.add(result.getInt(result
						.getColumnIndex(PURCHASE_DATE)));
				hasPurchased.add(result.getInt(result
						.getColumnIndex(HAS_PURCHASED)));
				totalCost.add(result.getFloat(result
						.getColumnIndex(TOTAL_COST)));
			} while (result.moveToNext());
		}
		if (result != null && !result.isClosed()) {
			result.close();
		}
		
		db.close();
	}
	
	public void updateTransaction(int transactionID) {
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(HAS_PURCHASED, 1);
		Log.d("CAme into here", "blah blah blah");
		db.update(TABLE_NAME, values, TRANSACTION_ID + "=" + transactionID, null);
		
		db.close();
	}

}
